package com.add;

import com.lineage.DatabaseFactory;
import com.lineage.server.datatables.ItemTable;
import com.lineage.server.model.Instance.L1ItemInstance;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.serverpackets.S_ServerMessage;
import com.lineage.server.serverpackets.S_SystemMessage;
import com.lineage.server.serverpackets.ServerBasePacket;
import com.lineage.server.utils.PerformanceTimer;
import com.lineage.server.world.World;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;
import java.util.StringTokenizer;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class L1DayPresent {
  private static final Log _log = LogFactory.getLog(L1DayPresent.class);
  
  private static final Map<Integer, ArrayList<L1DayPresent>> _buddyMap = new HashMap<>();
  
  private static ArrayList array = new ArrayList();
  
  private static boolean NO_MORE_GET_DATA15d = false;
  
  public static final String TOKEN = ",";
  
  public static void forTimeMessage(L1PcInstance pc) {
    ArrayList<Object> aTempData = null;
    if (!NO_MORE_GET_DATA15d) {
      NO_MORE_GET_DATA15d = true;
      getData15d();
    } 
    int i = 0;
    while (i < array.size()) {
      aTempData = (ArrayList<Object>) array.get(i);
      Calendar nowTime = Calendar.getInstance();
      if (((Integer)aTempData.get(0)).intValue() != 0 && ((Integer)aTempData.get(1)).intValue() != 0) {
        if (nowTime.get(2) + 1 == ((Integer)aTempData.get(0)).intValue() && 
          nowTime.get(5) == ((Integer)aTempData.get(1)).intValue()) {
          if (((Integer)aTempData.get(3)).intValue() != 0 && 
            pc.getLevel() < ((Integer)aTempData.get(3)).intValue())
            return; 
          if (((Integer)aTempData.get(2)).intValue() != 0) {
            byte class_id = 0;
            if (pc.isCrown()) {
              class_id = 1;
            } else if (pc.isKnight()) {
              class_id = 2;
            } else if (pc.isWizard()) {
              class_id = 3;
            } else if (pc.isElf()) {
              class_id = 4;
            } else if (pc.isDarkelf()) {
              class_id = 5;
            } else if (pc.isDragonKnight()) {
              class_id = 6;
            } else if (pc.isIllusionist()) {
              class_id = 7;
            } 
            if (((Integer)aTempData.get(2)).intValue() != class_id)
              return; 
          } 
          if ((int[])aTempData.get(5) != null && (int[])aTempData.get(6) != null) {
            int[] giveMaterials = (int[])aTempData.get(5);
            int[] giveCounts = (int[])aTempData.get(6);
            int l = 0;
            while (l < giveMaterials.length) {
              L1ItemInstance item = ItemTable.get().createItem(giveMaterials[l]);
              if (item.isStackable()) {
                item.setCount(giveCounts[l]);
              } else {
                item.setCount(1L);
              } 
              if (item != null) {
                if (pc.getInventory().checkAddItem(item, giveCounts[l]) == 0) {
                  pc.getInventory().storeItem(item);
                } else {
                  World.get().getInventory(pc.getX(), pc.getY(), pc.getMapId()).storeItem(item);
                } 
                pc.sendPackets((ServerBasePacket)new S_ServerMessage(403, item.getLogName()));
              } 
              l++;
            } 
          } 
          if ((String)aTempData.get(4) != null)
            pc.sendPackets((ServerBasePacket)new S_SystemMessage((String)aTempData.get(4))); 
        } 
      } else if (((Integer)aTempData.get(0)).intValue() == 0 && (
        (Integer)aTempData.get(1)).intValue() == 0) {
        if (((Integer)aTempData.get(3)).intValue() != 0 && 
          pc.getLevel() < ((Integer)aTempData.get(3)).intValue())
          return; 
        if (((Integer)aTempData.get(2)).intValue() != 0) {
          byte class_id = 0;
          if (pc.isCrown()) {
            class_id = 1;
          } else if (pc.isKnight()) {
            class_id = 2;
          } else if (pc.isWizard()) {
            class_id = 3;
          } else if (pc.isElf()) {
            class_id = 4;
          } else if (pc.isDarkelf()) {
            class_id = 5;
          } else if (pc.isDragonKnight()) {
            class_id = 6;
          } else if (pc.isIllusionist()) {
            class_id = 7;
          } 
          if (((Integer)aTempData.get(2)).intValue() != class_id)
            return; 
        } 
        if ((int[])aTempData.get(5) != null && (int[])aTempData.get(6) != null) {
          int[] giveMaterials = (int[])aTempData.get(5);
          int[] giveCounts = (int[])aTempData.get(6);
          int l = 0;
          while (l < giveMaterials.length) {
            L1ItemInstance item = ItemTable.get().createItem(giveMaterials[l]);
            if (item.isStackable()) {
              item.setCount(giveCounts[l]);
            } else {
              item.setCount(1L);
            } 
            if (item != null) {
              if (pc.getInventory().checkAddItem(item, giveCounts[l]) == 0) {
                pc.getInventory().storeItem(item);
              } else {
                World.get().getInventory(pc.getX(), pc.getY(), pc.getMapId()).storeItem(item);
              } 
              pc.sendPackets((ServerBasePacket)new S_ServerMessage(403, item.getLogName()));
            } 
            l++;
          } 
        } 
        if ((String)aTempData.get(4) != "")
          pc.sendPackets((ServerBasePacket)new S_SystemMessage((String)aTempData.get(4))); 
      } 
      i++;
    } 
  }
  
  private static void getData15d() {
    PerformanceTimer timer = new PerformanceTimer();
    Connection conn = null;
    try {
      conn = DatabaseFactory.get().getConnection();
      Statement stat = conn.createStatement();
      ResultSet rset = stat.executeQuery("SELECT * FROM w_节日活动");
      ArrayList<Object> arraylist = null;
      if (rset != null)
        while (rset.next()) {
          arraylist = new ArrayList();
          arraylist.add(0, new Integer(rset.getInt("month")));
          arraylist.add(1, new Integer(rset.getInt("day")));
          arraylist.add(2, new Integer(rset.getInt("checkClass")));
          arraylist.add(3, new Integer(rset.getInt("level")));
          arraylist.add(4, rset.getString("message"));
          if (rset.getString("new_item") != null && !rset.getString("new_item").equals("") && 
            !rset.getString("new_item").equals("0")) {
            arraylist.add(5, getArray(rset.getString("new_item"), ",", 1));
          } else {
            arraylist.add(5, null);
          } 
          if (rset.getString("new_item_counts") != null && !rset.getString("new_item_counts").equals("") && 
            !rset.getString("new_item_counts").equals("0")) {
            arraylist.add(6, getArray(rset.getString("new_item_counts"), ",", 1));
          } else {
            arraylist.add(6, null);
          } 
          array.add(arraylist);
        }  
      if (conn != null && !conn.isClosed())
        conn.close(); 
    } catch (Exception exception) {}
    _log.info("载入节日系统自动送礼: " + _buddyMap.size() + "(" + timer.get() + "ms)");
  }
  
  private static Object getArray(String s, String sToken, int iType) {
    StringTokenizer st = new StringTokenizer(s, sToken);
    int iSize = st.countTokens();
    String sTemp = null;
    if (iType == 1) {
      int[] iReturn = new int[iSize];
      int i = 0;
      while (i < iSize) {
        sTemp = st.nextToken();
        iReturn[i] = Integer.parseInt(sTemp);
        i++;
      } 
      return iReturn;
    } 
    if (iType == 2) {
      String[] sReturn = new String[iSize];
      int i = 0;
      while (i < iSize) {
        sTemp = st.nextToken();
        sReturn[i] = sTemp;
        i++;
      } 
      return sReturn;
    } 
    if (iType == 3) {
      String sReturn2 = null;
      int i = 0;
      while (i < iSize) {
        sTemp = sReturn2 = st.nextToken();
        i++;
      } 
      return sReturn2;
    } 
    return null;
  }
}
